//
//  HimWebVC.swift
//  xiaoleservice
//
//  Created by Wenzhe Fan on 2019/6/18.
//  Copyright © 2019年 Wenzhe Fan. All rights reserved.
//

import UIKit

class HimWebVC: UIViewController {
    var urlString:String = ""
    @IBOutlet weak var web: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        if urlString.isEmpty {
        
        }else{
//            let web = UIWebView.init(frame: self.view.frame)
//            self.view.addSubview(web)
            web.delegate = self as UIWebViewDelegate
            web.loadRequest(URLRequest.init(url: URL.init(string: urlString)!))
        }
    }
    @IBAction func home(_ sender: Any) {
        web.loadRequest(URLRequest.init(url: URL.init(string: urlString)!))
    }
    @IBAction func upgo(_ sender: Any) {
        web.goBack()
    }
    @IBAction func downgo(_ sender: Any) {
        web.goForward()
    }
    @IBAction func refres(_ sender: Any) {
        web.reload()
    }
    
}
extension HimWebVC:UIWebViewDelegate{
    public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool{
        switch navigationType {
       
        case .linkClicked:
            debugPrint("1",request.url?.absoluteString)
            
           break
        case .formSubmitted:
            debugPrint("2",request.url?.absoluteString)
            break
        case .backForward:
            debugPrint("3",request.url?.absoluteString)
            break
        case .reload:
            debugPrint("4",request.url?.absoluteString)
            break
        case .formResubmitted:
            debugPrint("5",request.url?.absoluteString)
            break
        case .other:
            debugPrint("6",request.url?.absoluteString)
            
            let url:String = request.url?.absoluteString ?? "22"
            if url.isEmpty {
                
            }else{
                if (url.contains(find: "mobileprovision")){
                    UIApplication.shared.openURL(request.url!)
                }
            }
            break
        }
        return true
    }
    public func webViewDidStartLoad(_ webView: UIWebView){
        
    }
    public func webViewDidFinishLoad(_ webView: UIWebView){
        
    }
    public func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
        
    }
}
extension String {
    func contains(find: String) -> Bool{
        return self.range(of: find) != nil
    }
    
    func containsIgnoringCase(find: String) -> Bool{
        return self.range(of: find, options: .caseInsensitive) != nil
    }
}
